import type { Technology } from '@/types/technology';
import { technologies as baseTechnologies } from '@/data/technology';
import { allKnowledgeCards } from '@/data/knowledge';

/**
 * 根据技术ID计算卡片数量
 */
function getCardCountByTechId(techId: string): number {
	// 根据技术ID筛选对应的知识卡片
	// 通过categoryId前缀判断技术栈：Java分类不带前缀，SQL分类带sql-前缀
	switch (techId) {
		case 'java':
			// Java知识卡片：categoryId不带sql-前缀
			return allKnowledgeCards.filter(card => !card.categoryId.startsWith('sql-')).length;
		case 'sql':
			// SQL知识卡片：categoryId带sql-前缀
			return allKnowledgeCards.filter(card => card.categoryId.startsWith('sql-')).length;
		default:
			return 0;
	}
}

/**
 * 获取技术列表（包含动态计算的cardCount）
 */
function getTechnologiesWithCardCount(): Technology[] {
	return baseTechnologies.map(tech => ({
		...tech,
		cardCount: getCardCountByTechId(tech.id)
	}));
}

/**
 * 根据技术ID获取技术信息
 */
export function getTechnologyById(id: string): Technology | undefined {
	const techs = getTechnologiesWithCardCount();
	return techs.find(tech => tech.id === id);
}

/**
 * 获取有卡片的技术列表
 */
export function getTechnologiesWithCards(): Technology[] {
	const techs = getTechnologiesWithCardCount();
	return techs.filter(tech => tech.cardCount > 0);
}

/**
 * 获取所有技术列表
 */
export function getAllTechnologies(): Technology[] {
	return getTechnologiesWithCardCount();
}

